import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Modal, Pagination, Popconfirm, Button } from 'antd';
import styles from './../Users.css';
import moment from 'moment';
import Factory from '../../factory';
import numeral from 'numeral';
import * as datetime from '../../../utils/datetime'
import dataEditCreate from '../../addons/DataEdit'
import dataTableCreate from '../../addons/DataTable'
import * as menuService from '../../../services/menus';
import * as com from '../../../utils/common';

const daoName = "CM_DS_GRLC";
const ButtonGroup = Button.Group;
const renderNumber = (num) => {
  let value = ''
  if (num) {
    value = numeral(num).format('0,0')
  }
  return value
};
const columns = [
  { title: '客户名称', width: 120, sorter: true, dataIndex: 'Khmc', key: 'Khmc', dispaly: true, filter: true },
  { title: '产品代码', width: 150, sorter: true, nowrap: true, caption: 'Cpbh', key: 'Cpbh', dispaly: true, filter: true },
  { title: '所属产品', width: 200, sorter: true, nowrap: true, caption: 'Sscp', key: 'Sscp', dispaly: true, filter: true },
  { title: '理财类型', width: 200, sorter: true, nowrap: true, caption: 'Lclx', key: 'Lclx', dispaly: true, filter: true },
  { title: '身份证号', width: 180, sorter: true, dataIndex: 'Sfzh', key: 'Sfzh', dispaly: true, filter: true },
  { title: '帐号', width: 160, sorter: true, dataIndex: 'Zhohao', key: 'Zhohao', dispaly: true, filter: true },
  { title: '卡号', width: 160, sorter: true, dataIndex: 'Khao', key: 'Khao', dispaly: true, filter: true },
  { title: '有效标志', width: 110, sorter: true, nowrap: true, caption: 'Yxbz', key: 'Yxbz', dispaly: true, filter: true },
  { title: '币种', width: 120, sorter: true, dataIndex: 'Bizhong', key: 'Bizhong', dispaly: true, filter: true },
  { title: '办理网点', width: 120, sorter: true, dataIndex: 'Blwd', key: 'Blwd', dispaly: true, filter: true },
  { title: '销售金额', className: 'column-money', caption: "Xsye", render: data => com.formatNumber(data.Xsye), key: 'Xsye', width: 120, sorter: true, dispaly: true, filter: true },
  { title: '余额', className: 'column-money', caption: "Ye", render: data => com.formatNumber(data.Ye), key: 'Ye', width: 120, sorter: true, dispaly: true, filter: true },
  { title: '销售日期', width: 110, sorter: true, key: 'Xsrq', caption: "Xsrq", dispaly: true, filter: true, render: data => datetime.format(data.Xsrq, "yyyy-MM-dd") },
  { title: '起息日', width: 110, sorter: true, key: 'Qxr', caption: "Qxr", dispaly: true, filter: true, render: data => datetime.format(data.Qxr, "yyyy-MM-dd") },
  { title: '到期日', width: 110, sorter: true, key: 'Dqr', caption: "Dqr", dispaly: true, filter: true, render: data => datetime.format(data.Dqr, "yyyy-MM-dd") },
  { title: '客户经理1', width: 250, sorter: false, dataIndex: 'YG1', key: 'YG1', dispaly: true, filter: false },
  { title: '分配比例1(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB1', render: data => renderNumber(data.ZB1), key: 'ZB1', dispaly: true, filter: false },
  { title: '客户经理2', width: 180, sorter: false, dataIndex: 'YG2', key: 'YG2', dispaly: true, filter: false },
  { title: '分配比例2(%)', width: 180, sorter: false, className: "column-money", caption: 'ZB2', render: data => renderNumber(data.ZB2), key: 'ZB2', dispaly: true, filter: false },
  { title: '客户经理3', width: 180, sorter: false, dataIndex: 'YG3', key: 'YG3', dispaly: true, filter: false },
  { title: '分配比例3(%)', width: 100, sorter: false, className: "column-money", caption: 'ZB3', render: data => renderNumber(data.ZB3), key: 'ZB3', dispaly: true, filter: false },
];
const controls = [
  { name: 'Khmc', required: true, type: 'text', label: '客户名称', grid: 12 / 24 },
  { name: 'Cpbh', required: true, type: 'text', label: '产品代码', grid: 12 / 24 },
  { name: 'Sscp', required: true, type: 'text', label: '所属产品', grid: 12 / 24 },
  { name: 'Lclx', required: true, type: 'text', label: '理财类型', grid: 12 / 24 },
  { name: 'Sfzh', required: true, type: 'text', label: '身份证号', grid: 12 / 24 },
  { name: 'Zhohao', required: true, type: 'text', label: '帐号', grid: 12 / 24 },
  { name: 'Khao', required: true, type: 'text', label: '卡号', grid: 12 / 24 },
  { name: 'Yxbz', required: true, type: 'text', label: '有效标志', grid: 12 / 24 },
  { name: 'Bizhong', required: true, type: 'text', label: '币种', grid: 12 / 24 },
  { name: 'Blwd', required: true, type: 'text', label: '办理网点', grid: 12 / 24 },
  { name: 'Xsye', required: true, type: 'number', label: '销售金额', grid: 12 / 24 },
  { name: 'Ye', required: true, type: 'number', label: '余额', grid: 12 / 24 },
  { name: 'Xsrq', required: true, type: 'number', label: '销售日期', grid: 12 / 24 },
  { name: 'Qxr', required: true, type: 'number', label: '起息日', grid: 12 / 24 },
  { name: 'Dqr', required: true, type: 'number', label: '到期日', grid: 12 / 24 },
];
const EditFrom = dataEditCreate(daoName);
const DataTable = dataTableCreate(daoName);
class CM_DS_GRLC extends Component {
  constructor(props) {
    super(props);
    this.state = {
      dataChange: false
    };
  }
  componentDidMount() {
    this.props.dispatch({
      type: `${daoName}/setKeyName`,
      payload: { keyName: "ID", permID: this.props.permID }

    });
    let sort = {
      field: 'Xsrq',
      order: 'descend'
    }
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, page: 1, pageSize: 20 ,sorter: sort}
    });

    this.loadOptionData();
  }
  loadOptionData() {
    let options = []
    controls.map(c => {
      if (c.fetch) {
        let f = c.fetch;
        if (c.type === 'cascader-select') {
          f.isCascader = true;
        }
        if (c.type !== 'search') {
          options.push(f);
        }
      }
    })
    if (daoName && options.length > 0) {
      this.props.dispatch({ type: `${daoName}/fetchOptions`, payload: { options } });
    }
  }
  findData() {
    const form = this.refs.findForm.getForm();
    let lx = '';
    let seeks = [];
    let whereItems = [];
    let WhereCauses = [];
    form.validateFields((err, values) => {
      if (!err) {
        lx = values.qygm;
      }
    });
    if (lx == 0) {
      seeks = [];
    } else {
      if (lx == 1) {
        whereItems.push({
          FieldName: "Lclx",
          Operator: "=",
          FieldValues: '保证收益理财产品'
        })
        seeks.push({
          WhereItems: whereItems
        })
      } else {
        if (lx == 2) {
          whereItems.push({
            FieldName: "Lclx",
            Operator: "=",
            FieldValues: '保本浮动收益理财产品'
          })
          seeks.push({
            WhereItems: whereItems
          })
        } else {
          if (lx == 3) {
            whereItems.push({
              FieldName: "Lclx",
              Operator: "=",
              FieldValues: '非保本浮动收益理财产品'
            })
            seeks.push({
              WhereItems: whereItems
            })
          }
        }
      }
    }
    this.props.dispatch({
      type: `${daoName}/fetch`,
      payload: { daoName, seeks }
    });
  }
  render() {
    let { showEdit, record, permID } = this.props;
    const controlsFilter = [
      {
        name: "qygm", label: "产品类型", type: "select", grid: 7 / 24, required: true,
        data: [{ value: '0', label: '全部' }, { value: '1', label: '保证收益理财产品' },
        { value: '2', label: '保本浮动收益理财产品' }, { value: '3', label: '非保本浮动收益理财产品' }],
        layout: { labelSpan: 8, wrapperSpan: 15 }
      },
      {
        name: 'CX', required: false, type: 'button-group', data: [{ value: '1', label: '查询', onClick: this.findData.bind(this) }], grid: 5 / 24
      },
    ];
    return (
      <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
        <div>
          <div className={styles.create}>
            <EditFrom ref="findForm" daoName={daoName} controls={controlsFilter} clearItemMargin={true} record={{ qygm: '全部' }} selectFirst={false} />
          </div>
          <DataTable ref="userTable" daoName={daoName} columns={columns} controls={controls} permID={permID} />
        </div>
      </div>
    )
  }
}
function mapStateToProps(state) {
  const { showEdit, record, selectedRowKeys, keyName } = state[daoName];
  return {
    showEdit,
    selectedRowKeys,
    keyName,
    record
  }
}
CM_DS_GRLC = connect(mapStateToProps)(CM_DS_GRLC)
export default CM_DS_GRLC;
let render = (props) => React.createElement(CM_DS_GRLC, props);
let f = new Factory()
f.set("CM_DS_GRLC", render)